package api

import (
	"context"
	"web/model"
	"web/proto/myarea"
	"web/utils"
)

func NewArea() *area {
	return &area{}
}

type area struct{}

func (e *area) MicroGetArea(ctx context.Context, req *myarea.Request, rsp *myarea.Response) error {
	//获取数据并返回给调用者
	areas, err := model.GetArea()
	if err != nil {
		rsp.Errno = utils.RECODE_DBERR
		rsp.Errmsg = utils.RecodeText(utils.RECODE_DBERR)
		return err
	}
	rsp.Errno = utils.RECODE_OK
	rsp.Errmsg = utils.RecodeText(utils.RECODE_OK)

	for _, v := range areas {
		var areaInfo myarea.AreaInfo
		areaInfo.Aid = int32(v.Id)
		areaInfo.Aname = v.Name

		rsp.Data = append(rsp.Data, &areaInfo)
	}

	return nil
}
